/*
 * @Description:
 * @Version:
 * @Author: Troubadour
 * @Date: 2023-02-02 13:51:37
 * @LastEditors: Troubadour 2276791354@qq.com
 * @LastEditTime: 2024-08-04 12:11:32
 */

#ifndef __INA226_OBJ_H__
#define __INA226_OBJ_H__

/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Include file                                                                       -
--------------------------------------------------------------------------------------------------------------------------------------*/
#include "main.h"
#include "SoftAnalogIIC.h"

/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  External macro                                                                     -
--------------------------------------------------------------------------------------------------------------------------------------*/

/* Configure register.
Bit15: Resets all registers to default values; this bit self-clears.
Bit14 ~ 12. Not used.
Bit11 ~ 9. Configuration Averaging Mode. Determines the number of samples that are collected and averaged.
    example: 0x00 = 1 sample, 0x01 = 4 samples, 0x02 = 16 samples, 0x03 = 64 samples,
        0x04 = 128 samples, 0x05 = 256 samples, 0x06 = 512 samples, 0x07 = 1024 samples.
Bit8 ~ 6. Configuration Bus Voltage Conversion Time. Sets the conversion time for the bus voltage measurement.
    example: 0x00 = 140us, 0x01 = 204us, 0x02 = 332us, 0x03 = 588us,
        0x04 = 1.1ms, 0x05 = 2.116ms, 0x06 = 4.156ms, 0x07 = 8.244ms.
Bit5 ~ 3. Configuration Shunt Voltage Conversion Time. Sets the conversion time for the shunt voltage measurement.
    example: 0x00 = 140us, 0x01 = 204us, 0x02 = 332us, 0x03 = 588us,
        0x04 = 1.1ms, 0x05 = 2.116ms, 0x06 = 4.156ms, 0x07 = 8.244ms.
Bit2 ~ 0. Configuration Operating Mode. Selects continuous, triggered, or power-down mode of operation.
    example: 0x00 = power-down mode, 0x01 = Shunt Voltage Triggered, 0x02 = Bus Voltage Triggered,
        0x03 = Bus and Shunt Voltage Triggered, 0x04 = Power-Down (or Shutdown), 0x05 = Shunt Voltage Continuous,
        0x06 = Bus Voltage Continuous, 0x07 = Bus and Shunt Voltage Continuous.
*/
#define INA266_CFG_REG(reset_cfg, AveragMode, BusVoltageConversionTime, ShuntVoltageConversionTime, OperatingMode)  \
    (((reset_cfg & 0x01) << 15) | \
    (4 << 12)                   | \
    ((AveragMode & 0x07) << 9)  | \
    ((BusVoltageConversionTime & 0x07) << 6) | \
    ((ShuntVoltageConversionTime & 0x07) << 3) | \
    (OperatingMode & 0x07))
/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  External enum                                                                      -
--------------------------------------------------------------------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  External structure                                                                 -
--------------------------------------------------------------------------------------------------------------------------------------*/

typedef struct
{
    uint16_t id;                    /* INA226的芯片唯一编号 */
    uint8_t addr;                   /* INA226的通信地址 */
    double current_lsb;             /* Current LSB. Maximum expected current / 0x8000 = Current LSB */
    double voltage;                 /* VBUS电压，单位：V */
    double shuntVoltage;            /* 分压电压，单位：uV */
    double current;                 /* 电流，单位：A */
    double power;                   /* 根据当前电流电压计算的功率，单位：wH */
    double total_power;             /* 根据当前功率计算的总功率 */
    double power_ins;               /* INA226内部计算的功率，单位：wH */
    double total_power_ins;         /* 根据INA226内部计算功率计算的总功率 */
    SoftAnalogIIC_Typedef *pIIC_Struct; /* Software analog IIC struct */
    /* Configure register. Suggest using the 'INA266_CFG_REG' macro to set the register.
    Bit15: Resets all registers to default values; this bit self-clears.
    Bit14 ~ 12. Not used.
    Bit11 ~ 9. Configuration Averaging Mode. Determines the number of samples that are collected and averaged.
        example: 0x00 = 1 sample, 0x01 = 4 samples, 0x02 = 16 samples, 0x03 = 64 samples,
            0x04 = 128 samples, 0x05 = 256 samples, 0x06 = 512 samples, 0x07 = 1024 samples.
    Bit8 ~ 6. Configuration Bus Voltage Conversion Time. Sets the conversion time for the bus voltage measurement.
        example: 0x00 = 140us, 0x01 = 204us, 0x02 = 332us, 0x03 = 588us,
            0x04 = 1.1ms, 0x05 = 2.116ms, 0x06 = 4.156ms, 0x07 = 8.244ms.
    Bit5 ~ 3. Configuration Shunt Voltage Conversion Time. Sets the conversion time for the shunt voltage measurement.
        example: 0x00 = 140us, 0x01 = 204us, 0x02 = 332us, 0x03 = 588us,
            0x04 = 1.1ms, 0x05 = 2.116ms, 0x06 = 4.156ms, 0x07 = 8.244ms.
    Bit2 ~ 0. Configuration Operating Mode. Selects continuous, triggered, or power-down mode of operation.
        example: 0x00 = power-down mode, 0x01 = Shunt Voltage Triggered, 0x02 = Bus Voltage Triggered,
            0x03 = Bus and Shunt Voltage Triggered, 0x04 = Power-Down (or Shutdown), 0x05 = Shunt Voltage Continuous,
            0x06 = Bus Voltage Continuous, 0x07 = Bus and Shunt Voltage Continuous.
    */
    uint16_t Cfg_register;
    uint16_t Calibration_value;     /* Calibration value */
}INA226TypeDef;



/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  External variables                                                                 -
--------------------------------------------------------------------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  External function                                                                  -
--------------------------------------------------------------------------------------------------------------------------------------*/

uint8_t INA226_Obj_Get_ID(INA226TypeDef *pIna226);
uint8_t INA226_Obj_Get_Current(INA226TypeDef *pIna226);
uint8_t INA226_Obj_Get_VbusVoltage(INA226TypeDef *pIna226);
uint8_t INA226_Obj_Get_ShuntVoltage(INA226TypeDef *pIna226);
uint8_t INA226_Obj_Get_PowerIns(INA226TypeDef *pIna226);
uint8_t INA226_Obj_Init(INA226TypeDef *ina226_struct);

#endif  /* __INA226_OBJ_H__ */
